<?php
/**
 * Created by PhpStorm.
 * User: xing
 * Date: 2018/10/8
 * Time: 下午1:33
 */
namespace app\wechat\controller;

use think\Config;
use think\Db;

class Share extends Common {

    /**
     * 分享商品
     * @return \think\response\Json
     */
    public function shareGoodsPage() {

        $currentCustomer = $this->request->currentCustomer;
        $goodsId = $this->request->post("goodsId");
        $customerId = $this->request->post("customerId");

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            $customer = $currentCustomer;
            //return errorReturn('errorWechat.errorCustomerNotFound'); //不提示错误  当没有customerId是默认为自己推荐
        }

        $stock = $this->stockService->findByCustomerIdAndGoodsId(
            $customer['id'], $goodsId);

        if (isNullOrEmpty($stock)) {

            // 当仓库内不存在  判断是否为推荐商品
            $recommendGoodsResult = getYxGoodsSearchList(
                1, 50, '-2', '');

            if(!$recommendGoodsResult["success"]) {
                return errorReturn('errorWechat.errorGoodsNotFound');
            }

            $isRecommend = false;
            foreach ($recommendGoodsResult['list']['list'] as $key=>$val) {

                if($goodsId == $val['goodsId']) {
                    $isRecommend = true;
                    break;
                }

            }

            if(!$isRecommend) {
                return errorReturn('errorWechat.errorGoodsNotFound');
            }

        }

        $yxGoodsResult = getYxGoodsSearchList('1', '1', '', $goodsId);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        $yxGoodsInfo = $yxGoodsResult['list']['list'][0];

        $yxGoodsShopPrice = $yxGoodsInfo['shopPrice'];
        // 判断是否为拼团商品  如果是则显示拼团价格
        if($yxGoodsInfo['isPintuan'] == 1) {
            $yxGoodsShopPrice = $yxGoodsInfo['tuanPrice'];
        }

        $returnData['customer_id'] = $customer['id'];
        $returnData['pid'] = $customer['yx_pid'];
        $returnData['headimgurl'] = $customer['headimgurl'];
        $returnData['nickname'] = strExceedingLength($customer['nickname']);
        $returnData['history_point'] = $customer['history_point'];
        $returnData['total_point'] = $customer['total_point'];
        $returnData['background_image'] = $customer['background_image']
            .config('constants.ossProcessResizeH');
        $returnData['goods_id'] = $yxGoodsInfo['goodsId'];
        $returnData['goods_name'] = $yxGoodsInfo['goodsName'];
        $returnData['shop_price'] = $yxGoodsShopPrice;
        $returnData['market_price'] = $yxGoodsInfo['marketPrice'];
        $returnData['little_desc'] = isNullOrEmpty($stock['little_desc'])
            ? '' : $stock['little_desc'];
        $returnData['goods_img'] = $yxGoodsInfo['goodsImg'];

        return successReturn($returnData);

    }

    /**
     * 分享别人的门店页
     * @return \think\response\Json
     */
    public function shareOtherStorePage() {

        $currentCustomer = $this->request->currentCustomer;
        $customerId = $this->request->post("customerId");

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            return errorReturn('errorWechat.errorCustomerNotFound');
        }

        $returnData['id'] = $customer['id'];
        $returnData['headimgurl'] = $customer['headimgurl'];
        $returnData['nickname'] = strExceedingLength($customer['nickname']);
        $returnData['history_point'] = $customer['history_point'];
        $returnData['total_point'] = $customer['total_point'];
        $returnData['background_image'] = $customer['background_image']
            .config('constants.ossProcessResizeH');

        return successReturn($returnData);

    }

}